//引入 搜索js
function importSearchJs(){
	var oHead = document.getElementsByTagName('HEAD').item(0); 
    var oScript= document.createElement("script"); 
    oScript.type = "text/javascript"; 
    //协议
    var protocol = location.protocol;
    // 域名
    var hostname = location.hostname;
	var hostport = location.host;
	//端口号
    if(hostname == "nation.chaoxing.com"){
    	hostname = "mobile.fanya.chaoxing.com";
    }
    oScript.src = protocol + "//" + hostname + ":" + hostport + "/js/commonSearch.js";
    oHead.appendChild(oScript); 
}

$(function() {
	importSearchJs();
	
	$("#password").keypress(function (event){
   		if(event.keyCode==13){
   			loginFrom();
   		} 
   	});
   	
   	$("#searchKey").keypress(function (event){
   		var domain = $("#unitdomain").val();
     	if(event.keyCode==13) {
     		searchAll(domain);
     	} 
    });
    
    $(".courseZone").hover(function(){
		$(this).find("ul").show();
	},function(){
		$(this).find("ul").hide();
	});
	
	$(".courseList dl").hover(function(){
		$(this).find(".vedio").show();	
	},function(){
		$(this).find(".vedio").hide();	
	});
	
	$(".nav td div").hover(function(){
		$(this).addClass("addBg");
		$(this).children(".navList").show();	
	},function(){
		$(this).removeClass("addBg");
		$(this).children(".navList").hide();	
	});
	
	// 河北政法使用
	$(".LoginPop1 .closed").click(function(){
		$(".LoginPop1").hide();
		$(".loginBghb").hide();
	});
	$(".LoginPop1 #loginBtn").click(function(){
		$(".LoginPop1").hide();
		$(".loginBghb").hide();
	});
	
});

function doSubmit(){	
	var uname = $("#uname"),password = $("#password");
	if ($.trim(uname.val()) == "" || $.trim(uname.val()) == "请输入用户名") {
		alert('请输入用户名!');
		uname.focus();
		return false;
	}
	if (password.val() == "" || password.val() == "请输入密码") {
		alert('请输入密码!');
		password.focus();
		return false;
	}
	return true;
}

function loginFrom(){
	if(doSubmit()) {
		var uname = $("#uname").val();
		/**
		 * 验证用户名是否为邮箱或手机号（邮箱或手机号，fid置为空）
		 */
		var regEmail = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi;
	    if(regEmail.test(uname)) {
	    	$("#loginform #fid").val("");
	    }
		var ph= /^(0|86|17951)?(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9])[0-9]{8}$/;
	    if(ph.test(uname)){
	    	$("#loginform #fid").val("");
	    }
		$("#loginform").submit();
	}
}

// 河北政法使用登录
function openlogin(){
	var wid = $(".LoginPop1").width();
	var Dwid = $(window).width();
	var left = (Dwid-wid)/2;
	$(".LoginPop1").css("left",left);
	var hit = $(".LoginPop1").height();
	var Dhit = $(window).height();
	var top = (Dhit-hit)/2;
	$(".LoginPop1").css("top",top);
	$(".LoginPop1").show();
	$(".loginBghb").show();
}
function doSubmithb(){	
	var uname = $("#unamehb"),password = $("#passwordhb");
	if (uname.val() == "" || uname.val() == "用户名" || uname.val() == "请输入用户名") {
		alert('请输入用户名!');
		uname.focus();
		return false;
	}
	if (password.val() == "" || password.val() == "密码" || password.val() == "请输入密码") {
		alert('请输入密码!');
		password.focus();
		return false;
	}
	//var email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	//$("#type").val(email.test(uname.val())?"email":"native");
	return true;
}

function loginFromhb(){
	if(doSubmithb()) {
		$("#loginFromhb").submit();
	}
}	


// 验证码方式登录
function loginCodeSubmit(id){
	var uname = $("#uname1"),password = $("#password1"),numcode=$("#numcode");
	if (uname.val() == "" || uname.val() == "用户名" || uname.val() == "请输入用户名") {
		alert('请输入用户名!');
		uname.focus();
		return false;
	}
	if (password.val() == "" || password.val() == "密码" || password.val() == "请输入密码") {
		alert('请输入密码!');
		password.focus();
		return false;
	}
	
	if(numcode.val() == ""){
		alert("请输入验证码");
		return false;
	}else if(numcode.val().length != 4){
		alert("验证码错误");
		return false;
	}
	$("#"+id).submit();
}


function updateBrower(id) {   
	$.ajax({
		type: "get",
		cache : 'false',
		url: "/portal/schoolCourseInfo/updateBrower?id="+id,
		dataType : "text",
		success : function(data, status) {
		},
		error : function(data, status, e) {
			alert(e);
		}
	});	
}

//tab切换
function setTab(name,cursel,n){
	for(i=1; i<=n; i++){
		var menu=document.getElementById(name+i);
		var con=document.getElementById("con_"+name+"_"+i);
		menu.className=i==cursel?"cur":"";
		con.style.display=i==cursel?"block":"none";
	}
}
function setTabCupl(name,cursel,n){
    for(i=1; i<=n; i++){
		var menu=document.getElementById(name+i);
		var con=document.getElementById("con_"+name+"_"+i);
		menu.className=i==cursel?"cur fr":"fr";
		con.style.display=i==cursel?"block":"none";
	}
}

// 滚动效果
var flag = "left";
function DY_scroll(wraper,prev,next,img,speed,or){  
	var wraper = $(wraper); 
	var prev = $(prev); 
	var next = $(next); 
	var img = $(img).find('ul'); 
	var w = img.find('li').outerWidth(true); 
	var s = speed; 
	next.click(function(){ 
		img.animate({'margin-left':-w}/*,1500,'easeOutBounce'*/,function(){ 
			img.find('li').eq(0).appendTo(img); 
			img.css({'margin-left':0}); 
		}); 
		flag = "left";
	}); 
	prev.click(function(){ 
		img.find('li:last').prependTo(img); 
		img.css({'margin-left':-w}); 
		img.animate({'margin-left':0}/*,1500,'easeOutBounce'*/); 
		flag = "right";
	}); 
	if (or == true){ 
		ad = setInterval(function() { flag == "left" ? next.click() : prev.click()},s*1000); 
		wraper.hover(function(){clearInterval(ad);},function(){ad = setInterval(function() {flag == "left" ? next.click() : prev.click()},s*1000);});
	} 
} 

//-->图片处理
$(function(){
	$("img").each(function(index,item){
		var src = item.getAttribute('src');
		// 如果有图片延迟加载，不处理
		var dataSrc = item.getAttribute('data-src');
		if(dataSrc) return;
		if(src == 'undefined' || src == "")  {
			item.src = "/public/img/img_default.png";
		}else {
			var oTempImg = new Image;
            oTempImg.onerror = function () {
            	item.src = "/public/img/img_default.png";
            };
            oTempImg.src = src;
            oTempImg = null;
		}
	});
	
});
